package com.itheima.d2_thread_safe;


public class ThreadTest2 {
    public static void main(String[] args) throws Exception {
        Account account = new Account("001", 100000.0);

        Person xm = new Person("小明", account);
        Person xh = new Person("小红", account);
        xm.start();
        xh.start();
        
    }
}

class Account {
    private String id;
    private Double money;

    public Account(String id, Double money) {
        this.id = id;
        this.money = money;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public void quMoney(double money) {
        synchronized(this) {
            Thread thread = Thread.currentThread();
            if (this.money >= money) {//有钱
                System.out.println(thread.getName() + "取成成功");
                this.money -= money;
                System.out.println("账户余额" + this.money);
            } else {
                System.out.println(thread.getName() + "取钱失败，余额不足！");
            }
        }
    }
}

class Person extends Thread {
    private Account account;

    public Person(String name, Account account) {
        super(name);
        this.account = account;
    }

    @Override
    public void run() {
        account.quMoney(100000);
    }
}